using System;

namespace Autorun
{
	public static class SqlConvert
	{
		private static System.Globalization.CultureInfo ci = Properties.Settings.Default.CultureInfo;

		#region "SqlString"
		public static string SqlString(string value)
		{
			if ((value == null) || (value.Length == 0))
				return "NULL";
			else
				return
					"'" + value.Replace("'", "''") + "'";
		}

		public static string SqlString(object value)
		{
			if ((value == null) || (value.ToString().Length == 0))
				return "NULL";
			else
				return "'" + value.ToString().Replace("'", "''") + "'";
		}
		#endregion

		#region "SqlStringOp"
		public static string SqlStringOp(string value)
		{
			return SqlStringOp(value, " IS ");
		}

		public static string SqlString(string value, string nullOperator)
		{
			if ((value == null) || (value.Length == 0))
				return string.Format("{0} NULL", nullOperator);
			else
				return string.Format("='{0}'", value.Replace("'", "''"));
		}

		public static string SqlStringOp(object value)
		{
			return SqlStringOp(value.ToString(), " IS ");
		}

		public static string SqlStringOp(object value, string nullOperator)
		{
			return SqlString(value.ToString(), nullOperator);
		}
		#endregion

		#region "SqlBool"
		public static string SqlBool(bool value)
		{
			return (value == true ? "1" : "0");
		}

		public static string SqBool(int value)
		{
			return (value == 0 ? "0" : "1");
		}
		#endregion

		#region "SqlId"
		public static string SqlId(int value)
		{
			if (value < 1)
				return "NULL";
			else
				return value.ToString();
		}
		#endregion

		#region "SqlIdOp"
		public static string SqlIdOp(int value)
		{
			return SqlIdOp(value, " IS ");
		}

		public static string SqlIdOp(int value, string nullOperator)
		{
			if (value < 1)
				return string.Format("{0} NULL", nullOperator);
			else
				return string.Format("='{0}'", value);
		}
		#endregion

		#region "SqlDate"
		public static string SqlDate(DateTime value)
		{
			if ((value == DateTime.MinValue) || (value == DateTime.MaxValue))
				return "NULL";
			else
				return string.Format("'{0}'", value.ToString("dd MMM yyyy HH:mm:ss", ci.DateTimeFormat));
		}

		public static string SqlDate(string value)
		{
			DateTime v;

			if (DateTime.TryParse(value, out v) == false)
				return "NULL";
			else
				return SqlDate(v);
		}

		public static string SqlDate(object value)
		{
			DateTime v;

			if (DateTime.TryParse(value.ToString(), out v) == false)
				throw new InvalidCastException();
			else
				return SqlDate(v);
		}
		#endregion

		#region "SqlDecimal"
		public static string SqlDecimal(decimal value)
		{
			return value.ToString(ci.NumberFormat);
		}

		public static string SqlDecimal(string value)
		{
			decimal v;

			if (decimal.TryParse(value, out v) == false)
				return "NULL";
			else
				return SqlDecimal(v);
		}

		public static string SqlDecimal(object value)
		{
			decimal v;

			if (decimal.TryParse(value.ToString(), out v) == false)
				throw new InvalidCastException();
			else
				return SqlDecimal(v);
		}
		#endregion

		#region "SqlPassword"
		public static string SqlPassword(string value)
		{
			return string.Format("'{0}'", BuildPassword(value));
		}

		public static string BuildPassword(string value)
		{
			return new Guid(new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(value))).ToString();
		}
		#endregion
	}
}
